package stream;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;

public class StreamTest {


    public static void main(String[] args) {

        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));

        List<String> names = menu.stream()
                .filter(dish -> {
                    System.out.println("filtering---" + dish.getName());
                    return dish.getCalories() > 300;
                })
                .map(dish -> {
                    System.out.println("mapping---" + dish.getName());
                    return dish.getName();
                })
                .limit(3)
                .collect(toList());
        System.out.println(names);

        Stream<Dish> dishStream = menu.stream();


        List<Integer> list = Arrays.asList(1, 2, 54, 6, 7);

        IntStream intStream = menu.stream().mapToInt(Dish::getCalories);
//        int sum = intStream.sum();

        final int a = 3;
        IntStream intStream1 = IntStream.range(1, 100).filter(b -> Math.sqrt(a * a + b * b) % 1 == 0);

        Stream<Integer> boxed1 = intStream1.boxed();

        Stream<int[]> streamArray = boxed1.map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)});

        streamArray.limit(5).forEach(t ->
                System.out.println(t[0] + ", " + t[1] + ", " + t[2]));


        IntStream intStream2 = IntStream.range(1, 100).filter(b -> Math.sqrt(a * a + b * b) % 1 == 0);
        Stream<int[]> stream = intStream2.mapToObj(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)});
        stream.limit(5).forEach(t ->
                System.out.println(t[0] + ", " + t[1] + ", " + t[2]));


        Stream<Integer> intStream3 = IntStream.range(1, 100).boxed();
        Stream<int[]> stream1 = intStream3.flatMap(aa -> IntStream.rangeClosed(aa, 100).filter(b -> Math.sqrt(aa * aa + b * b) % 1 == 0)
                .mapToObj(b -> new int[]{aa, b, (int) Math.sqrt(aa * aa + b * b)}));
        stream1.limit(5).forEach(t ->
                System.out.println(t[0] + ", " + t[1] + ", " + t[2]));

        IntSummaryStatistics collect = menu.stream().collect(summarizingInt(Dish::getCalories));

        String shortMenu = menu.stream().map(Dish::getName).collect(Collectors.joining());
        System.out.println(shortMenu);


        Collector<CharSequence, ?, String> joining = joining();
//        menu.stream().map(d-> d.getName()).collect();

        Map<Dish.Type, List<Dish>> collect1 = menu.stream().collect(groupingBy(Dish::getType));
        System.out.println(collect1);

        Map<Integer, List<Dish>> collect2 = menu.stream().collect(groupingBy(dish -> {
            if (dish.getCalories() <= 400) {
                return 4;
            } else if (dish.getCalories() <= 700) {
                return 7;
            } else {
                return 0;
            }
        }));
        System.out.println(collect2);

        Map<Boolean, List<Dish>> collect3 = menu.stream().collect(partitioningBy(Dish::isVegetarian));
        System.out.println(collect3);

        Stream<Integer> iterate = Stream.iterate(0, i -> i + 1);


    }




}
